package yxy.game.pm2.command.union.war;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.PlayerBean;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.UnionWarMessage;
import yxy.game.pm2.module.CurrencyHandle;
import yxy.game.pm2.module.union.UnionBean;
import yxy.game.pm2.module.union.UnionHandle;
import yxy.game.pm2.module.union.UnionHandle.UnionPlayerEntry;
import yxy.game.pm2.module.union.war.UnionWarUnionModule;

/** 联盟战 联盟战战果宝箱领取 */
@MessageType(proto = Command.UnionWarBoxGet, classtype = UnionWarMessage.UnionWarBoxGetRequest.class)
public class CMD_UnionWarBoxGet extends Command<UnionWarMessage.UnionWarBoxGetRequest> {

	@Override
	public void execute(MessageSource messageSource, UnionWarMessage.UnionWarBoxGetRequest request) {
		String playerCode = messageSource.getTag();
		// TODO 处理请求参数
		int id = request.getId();

		UnionWarMessage.UnionWarBoxGetResponse.Builder response = UnionWarMessage.UnionWarBoxGetResponse.newBuilder();

		// 检查是否已经有联盟
		UnionPlayerEntry entry = new UnionPlayerEntry(playerCode);
		{
			Result result = new UnionHandle().checkPlayerUnion(entry);
			if (!result.equals(Result.UnionIn)) {
				send(messageSource, response, result);
				return;
			}
		}
		UnionBean union = entry.getUnionBean();
		UnionWarUnionModule unionWarUnionModule = UnionWarUnionModule.get(union.getCode());
		if (unionWarUnionModule == null) {
			send(messageSource, response, Result.ParamError);// 宝箱不存在
			return;
		}

		if (!unionWarUnionModule.getEntrySet().contains(playerCode)) {
			send(messageSource, response, Result.UnionWarBoxForbid);// 没有领取资格
			return;
		}
		if (unionWarUnionModule.getBoxGetter().contains(playerCode)) {
			send(messageSource, response, Result.UnionWarBoxHasReward);// 你已经领过奖励
			return;
		}

		yxy.game.pm2.module.union.war.UnionWarBox box = unionWarUnionModule.getBoxes().get(id);
		if (box == null) {
			send(messageSource, response, Result.ParamError);// 宝箱不存在
			return;
		}
		if (box.getWinner() != null) {
			send(messageSource, response, Result.UnionWarBoxHasGet);// 宝箱已被领取
			return;
		}
		PlayerBean playerBean = PlayerBean.get(playerCode);
		new CurrencyHandle().addCurrency(playerCode, CCReason.联盟战, box.getReward());
		box.setWinner(playerBean.getName());
		unionWarUnionModule.getBoxGetter().add(playerCode);
		unionWarUnionModule.set();

		UnionWarMessage.UnionWarBox.Builder builder = UnionWarMessage.UnionWarBox.newBuilder();
		builder.setId(id);
		builder.setReward(box.getReward().getNumber());
		builder.setName(box.getWinner());
		response.setBox(builder);

		response.addRewards(new CurrencyHandle().packItem(box.getReward()));

		send(messageSource, response, Result.Success);
	}

	private void send(MessageSource messageSource, UnionWarMessage.UnionWarBoxGetResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}